#Project: TV polarization
#Author: Josh Ryan and Jeff Lyons
#Working Date: Winter 23
#Task: generate null effects plot
#Related files: "./Analysis Results/null_effects_analysis.xlsx is loaded, plot is saved as pdf


library(foreign)
library(effects)
library(sciplot)
library(sandwich)
library(lmtest)   
library(BMS)
library(rJava)
library(lattice)
library(plyr)
library(lme4)
library(gmodels)
library(MASS)
library(stringr)
library("XLConnect")
library(reshape2)
library(xlsx)
library(dplyr)
library(doBy)
library(Hmisc)
library(stargazer)
library(xtable)
library(dplyr)
library(xtable)
library(foreign)
library(dotwhisker)#https://cran.r-project.org/web/packages/dotwhisker/vignettes/dotwhisker-vignette.html
library(berryFunctions) #for the insert row function
library(ggplot2)
library(gridExtra)
library(wnominate)
library(tidyr)
library(wnominate)
library(oc)
library(haven)
library(PanelMatch)

#Figure 2 graphs
#this is just a dataset of effect sizes scaled in standard deviations
null.analysis<-read.xlsx("null_effects_analysis_v2.xlsx",1)

effects.plot<-ggplot(null.analysis, aes(order)) +
  geom_errorbar(aes(ymin = lcisd, ymax = ucisd), width = 0) +
  theme_classic() +
  scale_y_continuous(breaks=seq(-.4, .4, .1)) +
  coord_cartesian(ylim=c(-.4, .4)) +
  scale_x_continuous(breaks = null.analysis$order, labels = c("Budget Kurtosis", "Inter-party Polarization", "Democratic Intra-party Polarization", "Republican Intra-party Polarization", "Legislative Productivity")) + 
  geom_hline(yintercept=-.33, linetype="dashed", color = "gray")+
  geom_hline(yintercept=.33, linetype="dashed", color = "gray")+
  #coord_cartesian(xlim=c(0, 2)) +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  geom_hline(yintercept=0, color="gray")+
  xlab("Dependent Variables") + ylab("90% Confidence Intervals Relative to One SD of DV")+
  ggtitle("")+theme(plot.title = element_text(hjust = 0.5))

plot(effects.plot)
pdf("null_effects.pdf", width = 14, height = 8.5)
plot(effects.plot)
dev.off()


#this is for the legislator-level analysis Figure 7

null.analysis<-read.xlsx("null_effects_analysis_v2.xlsx",2)

effects.plot<-ggplot(null.analysis, aes(order)) +
  geom_errorbar(aes(ymin = lcisd, ymax = ucisd), width = 0) +
  theme_classic() +
  scale_y_continuous(breaks=seq(-.4, .4, .1)) +
  coord_cartesian(ylim=c(-.4, .4)) +
  scale_x_continuous(breaks = null.analysis$order, labels = c("Dem. Ideology", "GOP Ideology", "Legislative Effectiveness", "Effectiveness x Party", "Party Loyalty", "Loyalty x Party")) + 
  geom_hline(yintercept=-.33, linetype="dashed", color = "gray")+
  geom_hline(yintercept=.33, linetype="dashed", color = "gray")+
  #coord_cartesian(xlim=c(0, 2)) +
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))+
  theme(axis.line.x = element_line(color="black", size = .5),
        axis.line.y = element_line(color="black", size = .5))+
  geom_hline(yintercept=0, color="gray")+
  xlab("Dependent Variables") + ylab("90% Confidence Intervals Relative to One SD of DV")+
  ggtitle("")+theme(plot.title = element_text(hjust = 0.5))

plot(effects.plot)
pdf("null_effects_leglevel.pdf", width = 14, height = 8.5)
plot(effects.plot)
dev.off()

